package com.example.distinguishcolors.dao;

import com.example.distinguishcolors.LyyApplication;
import com.example.distinguishcolors.adapter.ColorAdapter;
import com.example.distinguishcolors.domain.GameRecord;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/10/4 10:21
 * @version 1.0
 */
public class GameRecordDao {
    private static volatile GameRecordDao dao;
    private static final LiteOrm liteOrm= LiteOrm.newSingleInstance(LyyApplication.getContext(),"colorGame.db");

    private GameRecordDao(){};

    public static GameRecordDao getInstance(){
        if(dao==null){
            synchronized (GameRecordDao.class){
                if(dao==null){
                    dao=new GameRecordDao();
                }
            }
        }
        return dao;
    }

    /**
     * 查询所有的记录
     * @return 按等级降序、花费时间升序、记录时间降序的游戏记录列表
     */
    public ArrayList<GameRecord> findAllByOrder(){
        ArrayList<GameRecord> resultList = liteOrm.query(new QueryBuilder<GameRecord>(GameRecord.class)
                .appendOrderDescBy("level")
                .appendOrderAscBy("spendTime")
                .appendOrderDescBy("timeMillis"));
        return resultList;
    }

    /**
     * 删除所有记录
     * @return 删除的记录数
     */
    public int deleteAll(){
        return liteOrm.deleteAll(GameRecord.class);
    }

    public long save(GameRecord gameRecord){
        return liteOrm.save(gameRecord);
    }
}
